#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "publicReader.h"

char *
readPublicKey(char * filename) {
	FILE * file;
	char * key;
	int c;
	int size = 0;
	int maxSize = 1000;

	file = fopen(filename, "r");
	if (file == NULL) {
		fprintf(stderr, "Error while opening: %s\n", filename);
		return NULL;
	}

	key = (char *) calloc(maxSize, sizeof(char));
	if (key == NULL) {
		fprintf(stderr, "Error while allocating memory\n");
		return NULL;
	}

	while((c = fgetc(file)) != EOF) {
		if (size == maxSize) {
			maxSize *= 2;
			key = (char *) realloc(key, maxSize * sizeof(char));
			if (key == NULL) {
				fprintf(stderr, "Error while allocating memory\n");
				return key;
			}
			key[size + 1] = 0; /*Realloc doesn't initialise the new pointer*/
		}
		
		key[size] = c;
		size++;
	}

	fclose(file);

	printf("%s******************\n", key);

	return key;
}
